import { ReactElement, useContext, useState, useEffect } from 'react'
import { Row, Col, Form } from 'antd'
import style from './index.module.less'
import { Context } from '../../context'
import UnitNumberInput from '@components/unitNumberInput'
import TextCell from '@src/components/textCell'
import Document from '../document'
import { awaitWrap } from '@src/assets/js/tool'
import { getDocumentListRequest } from './ajax'
import { CaseDocumentFullSchema } from '@components/schema/caseSchema'

const { Item } = Form

// 初始化可选文件列表
async function initDocumentList (setList: (v: CaseDocumentFullSchema[]) => void): Promise<void> {
  const [e, d] = await awaitWrap(getDocumentListRequest())
  if (e === null && d instanceof Array) {
    setList(d)
  }
}

function Main (): ReactElement {
  const [list, setList] = useState<CaseDocumentFullSchema[]>([])
  const { caseDetail: d } = useContext(Context)
  const disabled = d !== null && ['已抽查'].includes(d.score_status)
  useEffect(() => {
    initDocumentList(setList).catch(e => console.error(e))
  }, [])
  return (
    <div className={style.container} id='inputBlockSix'>
      <div className={style.title}>受理抽查</div>
      <Row>
        <Col span={10}>
          <Item label='1. 延迟制作/填写程序文书'>
            {disabled
              ? (
              <Item name='accepter_notice_list' noStyle><TextCell formatFn={(li) => `${li.title as string} - ${li.count as number}天`} /></Item>)
              : Document('accepter_notice_list', list, disabled)}
          </Item>
        </Col>
        <Col span={1}><span /></Col>
        <Col span={9}>
          <Item label='2. 受理属于不予受理案件的' name='accept_misaccept_count'>
            {disabled ? <TextCell unit='次' /> : <UnitNumberInput max={1} min={0} />}
          </Item>
        </Col>
      </Row>
      <Row>
        <Col span={9}>
          <Item label='3. 信息录入不准确的' name='accept_miscaseinfo_count'>
            {disabled ? <TextCell unit='次' /> : <UnitNumberInput />}
          </Item>
        </Col>
        <Col span={2}><span /></Col>
        <Col span={9}>
          <Item label='4. 遗失或损毁申请人提交的材料' name='accept_misfile_count'>
            {disabled ? <TextCell unit='次' /> : <UnitNumberInput />}
          </Item>
        </Col>
      </Row>
      <Row>
        <Col span={9}>
          <Item label='5. 未发现申请人提交的材料不齐、有误等情况' name='accept_mismaterial_count'>
            {disabled ? <TextCell unit='次' /> : <UnitNumberInput max={1} min={0} />}
          </Item>
        </Col>
        <Col span={2}><span /></Col>
        <Col span={9}>
          <Item label='6. 漏发、错发仲裁材料的' name='accept_mispostmaterial_count'>
            {disabled ? <TextCell unit='次' /> : <UnitNumberInput unit='次' />}
          </Item>
        </Col>
      </Row>
      <Row>
        <Col span={9}>
          <Item label='7. 计算受理费/处理费错误，造成经济损失的' name='accept_miscalculate_score'>
            {disabled ? <TextCell unit='分' /> : <UnitNumberInput max={-1} min={-5} unit='分' precision={2} />}
          </Item>
        </Col>
        <Col span={2}><span /></Col>
        <Col span={9}>
          <Item label='8. 案件类型的适用程序选用错误的' name='accept_misprocedure_count'>
            {disabled ? <TextCell unit='次' /> : <UnitNumberInput min={0} max={1} />}
          </Item>
        </Col>
      </Row>
      <Row>
        <Col span={9}>
          <Item label='9. 调解书撰写一般性文字错误' name='accept_typo_count'>
            {disabled ? <TextCell unit='处' /> : <UnitNumberInput unit='处' />}
          </Item>
        </Col>
        <Col span={2}><span /></Col>
        <Col span={9}>
          <Item label='10. 调解书撰写计算错误' name='accept_miscalculate_count'>
            {disabled ? <TextCell unit='处' /> : <UnitNumberInput unit='处' />}
          </Item>
        </Col>
      </Row>
    </div>
  )
}

export default Main
